package com.leyou.item.controller;

import com.leyou.common.domain.PageResult;
import com.leyou.dto.SkuDTO;
import com.leyou.dto.SpuDetailDTO;
import com.leyou.dto.SpuDto;
import com.leyou.item.entity.SpuDetail;
import com.leyou.item.service.GoodsService;
import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @version V1.0
 * @author: Ljxpa
 * @date: 2020/03/08 11:33
 * @description:
 */
@RestController
public class GoodsController {

	@Autowired
	private GoodsService goodsService;

	@GetMapping("/spu/page")
	public ResponseEntity<PageResult<SpuDto>> pageQuerySpu(
			@RequestParam(value = "key", required = false) String key,
			@RequestParam(value = "saleable", required = false) Boolean saleable,
			@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = "5") Integer rows
	) {
		return ResponseEntity.ok(goodsService.pageQuerySpu(key, saleable, page, rows));
	}

	/**
	 * 添加spu和sku
	 * @param spuDto 前端传递的包含spu和sku的一个实体对象
	 * @return
	 */
	@PostMapping("/goods")
	public ResponseEntity<Void> addGood(@RequestBody SpuDto spuDto){
		goodsService.addGood(spuDto);
		return ResponseEntity.status(HttpStatus.CREATED).build();
	}

	@PutMapping("/goods")
	public ResponseEntity<Void> editGood(@RequestBody SpuDto spuDto){
		goodsService.editGood(spuDto);
		return ResponseEntity.status(HttpStatus.CREATED).build();
	}

	/**
	 * 根据spuId查询对应的spuDetail
	 * @param spuId spuId
	 * @return spuDetailDto
	 */
	@GetMapping("/spudetail/{id}")
	public ResponseEntity<SpuDetailDTO> querySpuDetailBySpuId(@PathVariable("id")Long spuId){

		return ResponseEntity.ok(goodsService.querySpuDetailBySpuId(spuId));
	}

	/**
	 * 根据spuId查询所有的sku
	 * @param spuId spuId
	 * @return sku的list集合
	 */
	@GetMapping("/sku")
	public ResponseEntity<List<SkuDTO>> querySkuBySpuId(@RequestParam("spuId") Long spuId){
		List<SkuDTO> skuDTOList = goodsService.querySkuBySpuId(spuId);
		return ResponseEntity.ok(skuDTOList);
	}


	@GetMapping("/spu/detail")
	public ResponseEntity<SpuDetailDTO> querySpuBySpuId(@RequestParam("id")Long spuId){
		SpuDetailDTO spuDetailDTO = goodsService.querySpuDetailBySpuId(spuId);
		return ResponseEntity.ok(spuDetailDTO);
	}

	@GetMapping("sku/of/spu")
	public ResponseEntity<List<SkuDTO>> querySkusBySpuId(@RequestParam("id")Long spuId){
		List<SkuDTO> skuDTOList = goodsService.querySkuBySpuId(spuId);
		return ResponseEntity.ok(skuDTOList);
	}

	@PutMapping("/spu/saleable")
	public ResponseEntity<Void> updateSaleable(@RequestParam("id")Long spuId,@RequestParam("saleable")Boolean saleable){
		goodsService.updateSaleable(spuId,saleable);
		return ResponseEntity.status(HttpStatus.OK).build();
	}



}
